package com.coffee.rocketmq;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

/**
 * @author songkui
 * @since 2024/2/27 11:49
 */
@AllArgsConstructor
@Getter
@Slf4j
public class MochaMQTemplate {

    private final   EnvironmentProcessor environmentProcessor;
    private final   RocketMQTemplate rocketMQTemplate;

    public SendResult syncSend(String topic, String tag, MochaMQMessage<?> message){
        Message<?> sendMessage = MessageBuilder.withPayload(message).setHeader(RocketMQHeaders.KEYS, message.getKey()).build();
        return rocketMQTemplate.syncSend(environmentProcessor.destination(topic, tag), sendMessage);
    }
    public  void asyncSend(String topic, String tag, MochaMQMessage<?> message, SendCallback sendCallback){
        Message<?> sendMessage = MessageBuilder.withPayload(message).setHeader(RocketMQHeaders.KEYS, message.getKey()).build();
        rocketMQTemplate.asyncSend(environmentProcessor.destination(topic, tag), sendMessage, sendCallback);
    }

    //Delay message
    public SendResult syncSendDelayTimeMills(String topic, String tag, MochaMQMessage<?> message, long delayTimeMills){
        Message<?> sendMessage = MessageBuilder.withPayload(message).setHeader(RocketMQHeaders.KEYS, message.getKey()).build();
        return rocketMQTemplate.syncSendDelayTimeMills(environmentProcessor.destination(topic, tag), sendMessage, delayTimeMills);
    }
    public SendResult syncSendDelayTimeSeconds(String topic, String tag, MochaMQMessage<?> message, long delayTimeSeconds){
        Message<?> sendMessage = MessageBuilder.withPayload(message).setHeader(RocketMQHeaders.KEYS, message.getKey()).build();
        return rocketMQTemplate.syncSendDelayTimeSeconds(environmentProcessor.destination(topic, tag), sendMessage, delayTimeSeconds);
    }


    //Deliver
    public SendResult syncSendDeliverTimeMills(String topic, String tag, MochaMQMessage<?> message, long deliverTimeMills){
        Message<?> sendMessage = MessageBuilder.withPayload(message).setHeader(RocketMQHeaders.KEYS, message.getKey()).build();
        return rocketMQTemplate.syncSendDeliverTimeMills(environmentProcessor.destination(topic, tag), sendMessage, deliverTimeMills);
    }


    //orderly
    public SendResult syncSendOrderly(String topic, String tag, MochaMQMessage<?> message){
        Message<?> sendMessage = MessageBuilder.withPayload(message).setHeader(RocketMQHeaders.KEYS, message.getKey()).build();
        return rocketMQTemplate.syncSendOrderly(environmentProcessor.destination(topic, tag), sendMessage, message.getKey());
    }


}
